package cate.game.fight.msg;


import cate.common.table.d.GDFunc;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBFight;
import cate.game.role.fight.FuncPosition;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

@ClientMsg(title = "玩家阵型配置更新")
public class FuncPositionUpdateReq implements MsgInput {

    @NoteField(value = "单个功能的布阵")
    public FuncPosition funcPosition;

    @NoteField("是否要提示保存成功")
    public boolean notice;

    @Override
    public void execute(GameBody game, ClientContext cc) {
        if (funcPosition != null && funcPosition.funcId == GDFunc.ARENA_DEFEND) {
            game.notice.message(cc.getRole(), "非法功能");
            return;
        }
        EcResult<?> r = game.fight.position.updatePosition(cc.getRole(), funcPosition);
        if (!r.ok()) {
            game.notice.message(cc.getRole(), r.message);
        } else {
            // 历史记录
            cc.getRole().getHistory().action.funcPositionUpdate();
            cc.getRole().getHistory().action.positionSet(cc.getRole().getFight().maxFuncPositionNum());
            if (funcPosition.positions.stream().anyMatch(position -> position.hallowType != 0)) {
                cc.getRole().getHistory().action.onHallow();
            }

            if (notice) {
                game.notice.message(cc.getRole(), "保存布阵成功");
            }
            if (funcPosition.funcId == GDFunc.ADVENTURE) {
                cc.getRole().getBase().updatePower();
            }
        }
    }

    @Override
    public MsgConfig conf() {
        return MsgIdBFight.POSITION_UPDATE_REQ;
    }

}
